import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../components/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/ceshi',
    name: 'CeShi',
    component: () => import('../components/CeShi')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../components/Login')
  },
  {
    path: '/home',
    name: 'Home',
    redirect: 'welcome',
    component: () => import('../components/Home'),
    children: [
      {
        path: '/welcome',
        name: 'welcome',
        component: () => import('../components/Welcome')
      },
      {
        path: '/users',
        name: 'users',
        component: () => import('../components/user/Users')
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import('../components/power/Rights')
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import('../components/power/Roles')
      },
      {
        path: '/categories',
        name: 'categories',
        component: () => import('../components/goods/Categories')
      },
      {
        path: '/params',
        name: 'params',
        component: () => import('../components/goods/Params')
      },
      {
        path: '/goods',
        name: 'goods',
        component: () => import('../components/goods/GoodsList')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  // 如果是登录直接放行
  if (to.path === '/login') return next()
  // 获取token
  const tokenstr = window.sessionStorage.getItem('token')
  if (!tokenstr) {
    return next('/login')
  }
  next()
})

export default router
